import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

/*
 * Login.java
 *
 * Created on __DATE__, __TIME__
 */

/**
 *
 * @author  __USER__
 */
public class Login extends javax.swing.JFrame {
    private JLabel sfondobase;
    private JLabel sfondo;
	
	
	/** Creates new form Login */
	public Login() {
		initComponents();
                gestionegrafica();
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
	    this.setLocation(new Point((dimension.width - this.getSize().width) / 2, 
	    					  (dimension.height - this.getSize().height) / 2 ));//centra il frame
		this.user.requestFocusInWindow();
               
        }



    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        ok1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        pass = new javax.swing.JPasswordField();
        registra = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(153, 153, 255));
        setFont(new java.awt.Font("Bodoni MT Condensed", 1, 10)); // NOI18N
        setResizable(false);
        setUndecorated(true);

        jPanel3.setBackground(new java.awt.Color(51, 0, 255));
        jPanel3.setForeground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(153, 153, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 255, 255)));

        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("USER");

        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("PASS");

        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userActionPerformed(evt);
            }
        });
        user.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userKeyPressed(evt);
            }
        });

        ok1.setText("OK");
        ok1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ok1MouseClicked(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 255, 255)));

        jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Benvenuti inserire username e password ");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel2)
                .add(73, 73, 73))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(154, 154, 154)
                .add(jLabel2)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passKeyPressed(evt);
            }
        });

        registra.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        registra.setForeground(new java.awt.Color(255, 255, 255));
        registra.setText("Clicca qui per registrare un nuovo utente");
        registra.setBorder(null);
        registra.setBorderPainted(false);
        registra.setContentAreaFilled(false);
        registra.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                registraMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel4)
                        .add(18, 18, 18)
                        .add(user, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel5)
                        .add(18, 18, 18)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(registra)
                            .add(jPanel2Layout.createSequentialGroup()
                                .add(pass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(ok1)))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(166, Short.MAX_VALUE)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(user, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(7, 7, 7)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel5)
                            .add(pass, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                        .add(ok1)
                        .add(26, 26, 26)))
                .add(registra)
                .add(122, 122, 122))
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jButton1.setText("Exit");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addContainerGap(693, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(47, 47, 47)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jButton1)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_userActionPerformed

    private void ok1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ok1MouseClicked
    try{if (login(this.user.getText(),this.pass.getText())){
    new Menu().setVisible(true);
    }//inizializza il menu verificate le credenziali
    else{
    Errore Err = new Errore(this,true);
    Err.settitolo("Login Fallito");
    Err.setmessaggio("Ricontrollare le credenziali");
    Err.setVisible(true);
    }
    }
    catch(Exception e){
        System.out.println(e.toString());
     Errore Err = new Errore(this,true);
    Err.settitolo("Server non disponibile");
    Err.setmessaggio("Riprova più tardi");
    Err.setVisible(true);}
    }//GEN-LAST:event_ok1MouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
this.setVisible(false);        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    private void userKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userKeyPressed
       if (evt.getKeyCode() == KeyEvent.VK_DOWN | evt.getKeyCode() == KeyEvent.VK_ENTER) {
       this.pass.requestFocusInWindow();
    } // TODO add your handling code here:
    }//GEN-LAST:event_userKeyPressed

    private void passKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passKeyPressed
       try{
        if (evt.getKeyCode() == KeyEvent.VK_UP) {
       this.user.requestFocusInWindow();}
       if (evt.getKeyCode() == KeyEvent.VK_ENTER){ if (login(this.user.getText(),this.pass.getText())){
    new Menu().setVisible(true);
    }//inizializza il menu verificate le credenziali
    else{
    Errore Err = new Errore(this,true);
    Err.settitolo("Login Fallito");
    Err.setmessaggio("Ricontrollare le credenziali");
    }}
       }
       catch (Exception e){
     
           Errore Err = new Errore(this,true);
    Err.settitolo("Server non disponibile");
    Err.setmessaggio("Riprova più tardi");
    Err.setVisible(true);
       }// TODO add your handling code here:
    }//GEN-LAST:event_passKeyPressed

    private void registraMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_registraMouseClicked
     try{
     ControlloAccessoRegistra cr = new ControlloAccessoRegistra(this,true);
     cr.setVisible(true);
     }
      catch(Exception e){
        Errore Err = new Errore(this,true);
        Err.settitolo("Server non disponibile");
        Err.setmessaggio("Riprova più tardi ");
        Err.setVisible(true);
        
       
    }//GEN-LAST:event_registraMouseClicked
 
    
    }
    
    
    
    
    
    
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Login().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton ok1;
    private javax.swing.JPasswordField pass;
    private javax.swing.JButton registra;
    private javax.swing.JTextField user;
    // End of variables declaration//GEN-END:variables

    private void gestionegrafica(){

   
        
         
        sfondobase=new JLabel();  
        sfondobase.setBounds(0, 0, this.getWidth()+100, this.getHeight()+100);
        sfondobase.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sfondopannello.png"))); // NOI18N
        sfondobase.setToolTipText("");  
        sfondobase.setVisible(true);
        this.add(sfondobase);
        jPanel3.setOpaque(false);
         
         
        sfondo=new JLabel();  
        sfondo.setBounds(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
        sfondo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sfondologin.png"))); // NOI18N
        sfondo.setToolTipText("");  
        sfondo.setVisible(true);
        this.jPanel2.add(sfondo);
        jPanel2.setOpaque(false);
        jPanel1.setOpaque(false);
        
       
       
        
        
       
    }
    
    
    
    
    
    
    private static Boolean login(java.lang.String user, java.lang.String pass) {
        gruppo12.LoginService_Service service = new gruppo12.LoginService_Service();
        gruppo12.LoginService port = service.getLoginServicePort();
        return port.login(user, pass);
    }

}